package org.demo.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.json.JSONUtil;
import com.hellokaton.anima.core.ResultKey;
import com.hellokaton.anima.enums.OrderBy;
import com.hellokaton.anima.page.Page;
import com.hellokaton.blade.annotation.Path;
import com.hellokaton.blade.annotation.request.Body;
import com.hellokaton.blade.annotation.request.PathParam;
import com.hellokaton.blade.annotation.route.GET;
import com.hellokaton.blade.annotation.route.POST;
import com.hellokaton.blade.mvc.http.Request;
import com.hellokaton.blade.mvc.http.Response;
import com.hellokaton.blade.mvc.ui.RestResponse;
import org.demo.model.TaskInfo;


import java.util.List;
import java.util.stream.Collectors;

import static com.hellokaton.anima.Anima.*;

@Path
public class IndexController {

    @GET("/")
    public String index() {
        return "index.html";
    }
    @GET("/pendingTasks")
    public void pendingTasks(Request request, Response response) {
        String content=request.query("content","");
        int page= request.queryInt("page",1);
        int limit=request.queryInt("limit",10);
        Page<TaskInfo> pageList=select().from(TaskInfo.class).like("content","%"+content+"%").where(TaskInfo::getStatus,0).page(page, limit);
        response.json(pageList);
    }

    @GET("/completedTasks")
    public void completedTasks(Request request, Response response) {
        List<TaskInfo>list=select().from(TaskInfo.class).where(TaskInfo::getStatus,1).order(TaskInfo::getCompleteTime, OrderBy.DESC).all();
        response.json(list);
    }
    @GET("/deletedTasks")
    public void deletedTasks(Request request, Response response) {
        String content=request.query("content","");
        int page= request.queryInt("page",1);
        int limit=request.queryInt("limit",10);
        Page<TaskInfo> pageList=select().from(TaskInfo.class).like("content","%"+content+"%").where(TaskInfo::getStatus,2).page(page, limit);
        response.json(pageList);
    }

//    @POST("/addTask")
//    public RestResponse addTask(Request request) {
//        TaskInfo taskInfo=JSONUtil.toBean(request.bodyToString(),TaskInfo.class);
//        taskInfo.setCreateTime(DateUtil.date().getTime());
//        taskInfo.setId(UUID.fastUUID().toString().replace("-",""));
//        return RestResponse.ok(taskInfo.save().asInt());
//    }
    @POST("/addTask")
    public void addTask(@Body TaskInfo taskInfo, Response response) {
        taskInfo.setCreateTime(DateUtil.date().getTime());
        taskInfo.setId(UUID.fastUUID().toString().replace("-",""));
        taskInfo.setStatus(0);
        taskInfo.save();
        response.json(RestResponse.ok(null,200));
    }

    @POST("/editTask")
    public void editTask(@Body TaskInfo taskInfo, Response response) {
        taskInfo.setUpdateTime(DateUtil.date().getTime());
        taskInfo.update();
        response.json(RestResponse.ok(null,200));
    }
    @POST("/deleteTask")
    public void deleteTask(@Body TaskInfo taskInfo, Response response) {
        taskInfo.setStatus(2);
        taskInfo.update();
        response.json(RestResponse.ok(null,200));
    }
    @POST("/realDeleteTask")
    public void realDeleteTask(@Body TaskInfo taskInfo, Response response) {
        taskInfo.delete();
        response.json(RestResponse.ok(null,200));
    }

    @POST("/completeTask")
    public void completeTask(@Body TaskInfo taskInfo, Response response) {
        taskInfo.setCompleteTime(DateUtil.date().getTime());
        taskInfo.setStatus(1);
        taskInfo.update();
        response.json(RestResponse.ok(null,200));
    }

    @POST("/resetToPending")
    public void resetToPending(@Body TaskInfo taskInfo, Response response) {
        taskInfo.setStatus(0);
        taskInfo.setUpdateTime(DateUtil.date().getTime());
        taskInfo.update();
        response.json(RestResponse.ok(null,200));
    }

    @GET("login")
    public String login() {
        return "login.html";
    }
}
